package com.himilco.ocana.workflow.core.bo;

import java.util.Properties;
import java.util.ResourceBundle;
import javax.mail.*;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author oussema
 */
public class MailManager {
	private Boolean mailServieAvailable=true;
    
    public Boolean getMailServieAvailable() {
		return mailServieAvailable;
	}
	public void setMailServieAvailable(Boolean mailServieAvailable) {
		this.mailServieAvailable = mailServieAvailable;
	}
	public MailManager() {
		mailServieAvailable=true;
	}
	private static ResourceBundle smtpBundle = ResourceBundle.getBundle("smtp");
    /**
     * Envoi d'un email utilisant une socket SSL (SSLSocketFactory).
     * @param to celui ou ceux qui doivent recevoir l'email (séparation des adresses par des virgules)
     * @param subject sujet de l'email
     * @param content contenu de l'email
     * @throws AddressException les adresses de destinations sont incorrectes
     * @throws MessagingException une erreur est survenue à l'envoi de l'email
     */
     public static void sendEmailSSL(String to, String subject, String content) throws AddressException, MessagingException {
        // smtp properties
        Properties props = new Properties();
        props.put("mail.smtp.host", smtpBundle.getString("mail.smtp.host"));
        props.put("mail.smtp.socketFactory.port", smtpBundle.getString("mail.smtp.socketFactory.port"));
        props.put("mail.smtp.socketFactory.class", smtpBundle.getString("mail.smtp.socketFactory.class"));
        props.put("mail.smtp.auth", smtpBundle.getString("mail.smtp.auth"));
        props.put("mail.smtp.port", smtpBundle.getString("mail.smtp.port"));
        // authentification
        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() {
            @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(smtpBundle.getString("mail.session.user"), smtpBundle.getString("mail.session.pass"));
                }
            }
        );
        // construct message 
        Message message = new MimeMessage(session);
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        message.setSubject(subject);
        message.setContent(content, "text/html; charset=ISO-8859-1");
        // send email
        Transport.send(message);
    }
    public static void main(String[] args) throws AddressException, MessagingException {
        sendEmailSSL("oussema.toukebri@gmail.com", "Test ", "Hello oussema");
    } 
    
    public void sendNotifMail(){
    	 try {
			sendEmailSSL("projects.ocana@gmail.com", "Test ", "5mn condition reached");
		} catch (AddressException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
    }
    
    public void sendMailToServiceProvider(){
   	 try {
			sendEmailSSL("projects.ocana@gmail.com", "Service Provider Service", "dispatched mail asking for SP support ");
		} catch (AddressException e) {
			// TODO Auto-generated catch block 
			e.printStackTrace();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
   }
    
}
    

